package filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class LoginFilter implements Filter{

	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain fc) throws IOException, ServletException {
		HttpServletRequest httpRequest=
				(HttpServletRequest) req;
		String uri=httpRequest.getRequestURI();
		System.out.println("filter  uri : " + uri);
		
		if(uri.endsWith(".do")){
			HttpSession session=httpRequest.getSession();
			Object idObject=session.getAttribute("id");
			if(idObject==null){
				System.out.println("로그인을 안했구만..");  
				httpRequest.setAttribute("login", "login/loginForm.jsp");
			}else{
				System.out.println("로그인을 했구나..");
				httpRequest.setAttribute("login", "login/loginSuccess.jsp");
			} 
		}
		// 가던 길 가세요    
		fc.doFilter(httpRequest, res);  
	}
	@Override
	public void destroy() {
	}


	@Override
	public void init(FilterConfig arg0) throws ServletException {
		
	}

}
